package com.app.mycomposedemo.ui.components


import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.pager.VerticalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.app.mycomposedemo.ui.theme.Blue700
import com.app.mycomposedemo.ui.theme.Gray66
import com.app.mycomposedemo.ui.theme.Gray200
import com.app.mycomposedemo.ui.theme.Gray33
import com.app.mycomposedemo.viewmodel.MainViewModel
import kotlinx.coroutines.launch
import java.util.Timer
import java.util.TimerTask

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun VerticalPagerComp(vm: MainViewModel) {

    // 用于循环轮播的虚拟count,使用最大值是为了达到循环轮播的目的
    val fakeCount: Int = Int.MAX_VALUE
    // 实际轮播内容的count
    val actualCount: Int = vm.verticalTexts.size
    // 初始初始轮播图的下标
    val initIndex: Int = fakeCount / 2
    // 记录轮播的下标
    val pagerState = rememberPagerState(initialPage = initIndex)

    // 获取组合感知作用域，以便在可组合项外启动协程
    val coroutineScope = rememberCoroutineScope()

    // 销毁时附带效应
    DisposableEffect(Unit) {
        val timer = Timer()

        timer.schedule(object : TimerTask() {
            override fun run() {
                coroutineScope.launch { pagerState.animateScrollToPage(pagerState.currentPage + 1) }
            }
        }, 3000, 3000)

        // 页面销毁时调用
        onDispose {
            timer.cancel()
        }
    }

    // 垂直轮播图
    Row(
        modifier = Modifier
            .padding(16.dp, 8.dp)
            .clip(RoundedCornerShape(8.dp))
            .fillMaxWidth()
            .background(Gray200)
            .height(45.dp),
        horizontalArrangement = Arrangement.Start,
        verticalAlignment = Alignment.CenterVertically
    ) {
        Text(
            modifier = Modifier.padding(start = 24.dp, end = 8.dp),
            text = "最新咨询",
            color = Blue700,
            fontWeight = FontWeight.Bold
        )
        VerticalPager(
            modifier = Modifier
                .padding(start = 8.dp)
                .weight(1f),
            pageCount = fakeCount,
            state = pagerState,
            horizontalAlignment = Alignment.Start
        ) { index ->
            //index - (index.floorDiv(actualCount)) * actualCount
            // 计算当前显示的轮播下标
            val actualIndex = (index - initIndex).floorMod(actualCount)
            Text(
                text = vm.verticalTexts[actualIndex],
                maxLines = 1,
                color = Gray33,
                fontSize = 14.sp,
                overflow = TextOverflow.Ellipsis
            )
        }
        Text(
            modifier = Modifier.padding(start = 8.dp, end = 24.dp),
            text = "更多",
            color = Gray66,
        )
    }
}

private fun Int.floorMod(other: Int): Int = when (other) {
    0 -> this
    //将虚拟数据按照实际数据总数分为 N 组
    //当前虚拟下标是在这虚拟数据中的哪一组：虚拟下标floorDiv实际数据总数(虚拟下标/实际数据总数)。向下取整
    //虚拟下标 - (虚拟下标/实际数据总数) * 实际数据总数
    else -> this - floorDiv(other) * other
}


@Preview
@Composable
fun VerticalPagerCompPreview() {
    VerticalPagerComp(MainViewModel())
}

